/**
 * 浏览器全屏切换
 * @param el dom
 * @param fullscreen 是否全屏
 * @returns {boolean} 全屏状态
 */
export function toggleFullscreen (el, fullscreen) {
  if (typeof el === 'undefined' || el === null) {
    el = document.documentElement
  }
  if (typeof fullscreen === 'undefined' || fullscreen === null) {
    fullscreen = !isFullscreen()
  }
  if (fullscreen) {
    const rfs =
      el.requestFullscreen ||
      el.requestFullScreen ||
      el.webkitRequestFullScreen ||
      el.mozRequestFullScreen ||
      el.msRequestFullScreen
    if (rfs) {
      rfs.call(el)
    } else {
      throw new Error('您的浏览器不支持全屏模式')
    }
  } else {
    const cfs =
      document.exitFullscreen ||
      document.exitFullScreen ||
      document.webkitCancelFullScreen ||
      document.mozCancelFullScreen ||
      document.msExitFullscreen
    cfs && cfs.call(document)
  }
  return fullscreen
}

/**
 * 获取当前是否是全屏状态
 * @returns {boolean}
 */
export function isFullscreen () {
  return !!(
    document.fullscreenElement ||
    document.webkitFullscreenElement ||
    document.mozFullScreenElement ||
    document.msFullscreenElement
  )
}
